<?php
session_start();
if (!isset($_SESSION["email"])) {
    header('Location: login.php');
}else{
  if ($_SESSION["email"]!="programming.iter@gmail.com") {
     header('Location: login.php');
  }
}
?>
<?php include("incs/top.php"); ?>

    <div class="container-fluid">
      <div class="row">
        <?php include("incs/sidebar.php"); ?>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1 class="page-header">Danh Mục Bài Viết</h1>
          <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>Tên danh mục</th>
                  <th>Link danh mục</th>
                  <th>Trạng thái</th>
                  <th>Active</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td> <input type="text"  id="cat_name" name="cat_name" placeholder="Tên danh mục..." ></td>
                  <td> <input type="text"  id="cat_link" name="cat_link" placeholder="Link danh mục" ></td>
                  <td>
                      <select id="cat_public" name="cat_public">
                        <option value="0">Unpublic</option>
                        <option value="1">Public</option>  
                      </select>
                  </td>
                  <td> <button type="button" id="btn_save_new_cat" class="btn btn-primary">Thêm</button></td>
                </tr>
              </tbody>
            </table>
          </div>
          
          <h2 class="sub-header">Danh sách danh mục</h2>
          <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>ID</th>
                  <th>Tên danh mục</th>
                  <th>Ngày tạo</th>
                  <th>Người tạo</th>
                </tr>
              </thead>
              <tbody>
                <?php
                  include("../lib/connection.php");
                  $sql = "SELECT * FROM category";
                  $query = mysqli_query($conn,$sql);
                  $TRs = "";
                  while ($row = mysqli_fetch_array($query)) {
                    $cat_id = $row['cat_id'];
                    $cat_name = $row['cat_name'];
                    $inp_date = $row['inp_date'];
                    $upd_user = $row['upd_user'];
                    $TRs .= "<tr><td>$cat_id</td><td>$cat_name</td><td>$inp_date</td><td>$upd_user</td></tr>";
                  }
                  echo $TRs;
                ?>
             
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="../bootstrap/js/bootstrap.min.js"></script>
    <script src="../bootstrap/js/docs.min.js"></script>
    <script src="js/common.js"></script>
    <script type="text/javascript">
      $("#btn_save_new_cat").on("click",function(){
        var cat_name = $("#cat_name").val();
        var cat_link = $("#cat_link").val();
        var cat_public = $("#cat_public").val();
        $.ajax({
          url : "act/processs_save_cat.php?flg_add_cat",
          type : "POST",
          dataType : "json",
          data : {cat_name: cat_name, cat_public: cat_public, cat_link: cat_link},
          success : function(response){
            if (response.status=="200") {
              location.reload();
            };
            
          }
        });
      })
    </script>
   
  </body>
</html>